package com.easysoft.framework.announce.service;

import com.easysoft.commons.utils.UUIDUtils;
import com.easysoft.framework.annotation.jdbc.DataSource;
import com.easysoft.framework.announce.mapper.SysAnnounceMapperExtd;
import com.easysoft.framework.announce.model.SysAnnounce;
import com.easysoft.framework.announce.model.SysAnnounceQuery;
import com.easysoft.framework.announce.model.SysAnnounceView;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.manager.model.SysManagerView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("service.AnnounceService")
public class AnnounceService {
    @Autowired
    @Qualifier("mapper.SysAnnounceMapperExtd")
    private SysAnnounceMapperExtd sysAnnounceMapperExtd;


    @DataSource(DataSource.SLAVE)
    public List<SysAnnounceView> selectSysAnnounceByPage(Page page, SysAnnounceQuery query) {
        int maxRow = sysAnnounceMapperExtd.selectCountAllAnnounce(query);
        if (maxRow == 0){
            return new ArrayList<SysAnnounceView>();
        }
        page.setMaxRow(maxRow);
        return sysAnnounceMapperExtd.selectSysAnnounceByPage(page,query);
    }

    @DataSource(DataSource.MASTER)
    public int addSysAnnounce(SysAnnounce announce) {
        announce.setId(UUIDUtils.getUUID());
        announce.setCreateTime(new Date());
        return sysAnnounceMapperExtd.insertSelective(announce);
    }

    @DataSource(DataSource.SLAVE)
    public SysAnnounce selectSysAnnounceById(String id) {
        return sysAnnounceMapperExtd.selectByPrimaryKey(id);
    }

    @DataSource(DataSource.MASTER)
    public int updateSysAnnouce(SysAnnounce announce){
        return sysAnnounceMapperExtd.updateByPrimaryKeySelective(announce);
    }

    @DataSource(DataSource.MASTER)
    public int deleteSysAnnounce(String id) {
        return sysAnnounceMapperExtd.deleteByPrimaryKey(id);
    }

    @DataSource(DataSource.SLAVE)
    public SysAnnounceView selectSysAnnounceViewByPrimaryKey(String id){
        return sysAnnounceMapperExtd.selectSysAnnounceViewByPrimaryKey(id);
    }

    public List<Map<String, String>> selectSysAnnounce(boolean isCaeousel) {
        return sysAnnounceMapperExtd.selectSysAnnounce(isCaeousel);
    }
}
